package com.Policing.Back;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Color;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.SearchView;
import android.widget.Toast;

import androidx.annotation.Nullable;

import com.Policing.Ip_set.Ip_Set;
import com.Policing.Listview_animation.BounceLoadingView;
import com.Policing.Okhttps.HttpOkHttp;
import com.Policing.R;
import com.Policing.bean.Department_bean;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2018/10/16.
 */

public class userbackQuery extends Activity {

    private SearchView sv;
    private ListView lv;
    private List<String> X=new ArrayList<>();
    private List<String > p = new ArrayList<String>();
    private String url,id,assetNumber;
    private BounceLoadingView loadingView;
    private Object[] names;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.add_department);
        url = "http://"+ Ip_Set.IP+"/api/returnsUserInspect";
        //url = "http://192.168.1.159:8087/api/getAllPut";
        SharedPreferences mm = getSharedPreferences("mm", Context.MODE_PRIVATE);//1.通过上下文得到
        id = mm.getString("id","");
        assetNumber = getIntent().getStringExtra("assetNumber");
        lv = (ListView) findViewById(R.id.lv_allot_sl);
        lv.setTextFilterEnabled(true);//设置lv可以被过虑
        sv = (SearchView) findViewById(R.id.tv_allot_sl);
        loadingView = (BounceLoadingView) findViewById(R.id.loadingView);
        loadingView.addBitmap(R.mipmap.v4);
        loadingView.addBitmap(R.mipmap.v5);
        loadingView.addBitmap(R.mipmap.v6);
        loadingView.addBitmap(R.mipmap.v7);
        loadingView.addBitmap(R.mipmap.v8);
        loadingView.addBitmap(R.mipmap.v9);
        loadingView.setShadowColor(Color.LTGRAY);
        loadingView.setDuration(700);
        loadingView.start();
        Thread1();
        // 设置该SearchView默认是否自动缩小为图标
        sv.setIconifiedByDefault(false);
        sv.setQueryHint("请选择归还人！！");
        // 为该SearchView组件设置事件监听器
        // 设置该SearchView显示搜索按钮
        sv.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
            @Override
            public boolean onQueryTextSubmit(String query) {
                // 实际应用中应该在该方法内执行实际查询
                // 此处仅使用Toast显示用户输入的查询内容
                Toast.makeText(userbackQuery.this, "您的选择是:" + query, Toast.LENGTH_SHORT).show();
                return false;
            }

            @Override
            public boolean onQueryTextChange(String newText) {
                Object[] obj = searchItem(newText);
                updateLayout(obj);
                return false;

            }
        });
        lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                String name = (String)adapterView.getItemAtPosition(i);//获取当前点击的item的字符串内容
                String id = "";
                for (int j = 0;j<X.size();j++){
                    if (X.get(j).equals(name.trim())){
                        id = p.get(j) ;
                    }

                }
                Log.i("123123",name);
                Log.i("123123",id);
                Intent intent = new Intent();
                intent.putExtra("admin_name",name);
                intent.putExtra("admin_id",id);
                intent.putExtra("assetNumber",assetNumber);
                userbackQuery.this.setResult(7,intent);
                userbackQuery.this.finish();//把item的内容返回给之前的activity
            }
        });

    }

    private void Thread1() {
        Map<String,String> map = new HashMap<>();
        map.put("id",id);
        map.put("assetNumber",assetNumber);
        HttpOkHttp.getInstance().requestPost(url, map, new HttpOkHttp.OkHttpCallBack<String>() {
                    @Override
                    public void requestSuccess(String  responseData) {
                        try {
                            JSONObject jsonObject = new JSONObject(responseData);
                            if (jsonObject.getString("state").equals("1")){
                                JSONArray  jsonArray = jsonObject.getJSONArray("users");
                                Gson gson = new Gson();
                                List<Department_bean> userList = gson.fromJson(String.valueOf(jsonArray), new TypeToken<List<Department_bean>>(){}.getType());
                                for(int i = 0;i < userList.size();i++){
                                    X.add(userList.get(i).getName());
                                    p.add(userList.get(i).getId());
                                }
                                //----------------------------------------------
                             runOnUiThread(new Runnable() {
                                 @Override
                                 public void run() {
                                     loadingView.setVisibility(View.GONE);
                                     names = X.toArray();
                                     lv.setAdapter(new ArrayAdapter<Object>(getApplicationContext(),
                                             R.layout.listview_style,names));
                                 }
                             });
                            }else {
                              runOnUiThread(new Runnable() {
                                  @Override
                                  public void run() {
                                      finish();
                                      Toast.makeText(userbackQuery.this,"此编码无在用资产",Toast.LENGTH_SHORT).show();
                                  }
                              });
                            }

                        } catch (JSONException e) {
                            e.printStackTrace();
                        }

                    }

                    @Override
                    public void requestFailure(String message) {

                    }
                },String.class);
    }
    public void updateLayout(Object[] obj) {
        lv.setAdapter(new ArrayAdapter<Object>(getApplicationContext(),
                R.layout.listview_style, obj));
    }
    public Object[] searchItem(String name) {
        ArrayList<String> mSearchList = new ArrayList<String>();
        for (int i = 0; i < X.size(); i++) {
            int index = X.get(i).indexOf(name);
            // 存在匹配的数据
            if (index != -1) {
                mSearchList.add(X.get(i));
            }
        }
        return mSearchList.toArray();
    }
}
